package com.jee.ssm.modules.ssm.controller;
import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.model.User;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.modules.ssm.services.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import sun.util.resources.cldr.ga.TimeZoneNames_ga;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends AdminBaseController<User>{

    /**
     * 进入用户添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("user:add")
    public ModelAndView add(ModelMap model){
        model.put("longId", UUIDFactory.getStringId());

        return new ModelAndView("manager/user/add",model);
    }
    /**
     * 进入用户编辑页面
     * @param model 返回用户的容器
     * @param id 用户id
     * @return 编辑页面
     * @throws Exception 进入编辑页面失败
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("user:edit")
    public ModelAndView edit(ModelMap model, @RequestParam String id) {
        model.put("data",userService.selectById(id));
        return new ModelAndView("manager/user/edit",model);
    }
    /**
     * 角色添加
     * @param user 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据添加异常
     */
    @ResponseBody
    @RequestMapping(value="/save")
    @AdminControllerLog(description = "用户添加" )
    @RequiresPermissions("user:add")
    public Tip save(User user,HttpServletRequest request) throws Exception{
        if(!user.getFileurl().equals("")) {
            String url = "";
            String[] images = user.getFileurl().split(",");
            for (int i = 0; i < images.length; i++) {
                //临时文件
                File file = new File(images[i]);
                //图片地址 相对路径
                String path = "/images/fileurl/"
                        + System.currentTimeMillis()+"_"
                        + "url"
                        + i
                        + file.getName().substring(file.getName().lastIndexOf("."));
                //图片地址 绝对路径
                String realPath = request.getServletContext().getRealPath(path);
                //保存图片
                ImageIO.write(ImageIO.read(file), "jpg", new File(realPath));
                url += url.equals("") ? path : "," + path;
                user.setFileurl(url);
            }
        }
        userService.insert(user);
        return new Tip();
    }

    /**
     * 根据 id 修改用户
     * @param user 带id的用户对象
     * @return 成功状态
     * @throws Exception 数据修改异常
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @AdminControllerLog(description = "用户编辑" )
    @RequiresPermissions("user:edit")
    public Tip update(User user,HttpServletRequest request) throws Exception {
        if(!user.getFileurl().equals("")) {
            String url = "";
            String[] images=user.getFileurl().split(",");
            for (int i = 0; i < images.length; i++) {
                //临时文件
                File file = new File(images[i]);
                //图片地址 相对路径
                String path = "/images/fileurl/"
                        + System.currentTimeMillis()+"_"
                        +"url"
                        + i
                        + file.getName().substring(file.getName().lastIndexOf("."));
                //图片地址 绝对路径
                String realPath = request.getServletContext().getRealPath(path);
                ImageIO.write(ImageIO.read(file), "jpg", new File(realPath));
                url += url.equals("") ? path : "," + path;
                user.setFileurl(url);
            }
        }
        userService.updateById(user);
        return new Tip();
    }
    /**
     * 根据 id 删除用户
     * @param id 用户id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @AdminControllerLog(description = "删除用户" )
    @RequiresPermissions("user:delete")
    public Tip delete(@RequestParam String id) {

        try {
            userService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 列表批量删除
     * @param ids 用户id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @AdminControllerLog(description = "批量删除用户" )
    @RequiresPermissions("user:delete")
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            userService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1);
        }
    }
    /**
     * 根据 id 查找用户
     * @param id 用户id
     * @return 用户对象 jsonj
     * @throws Exception 查找用户失败
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    public User find(@RequestParam String id) {

        return userService.selectById(id);
    }

    /**
     * 获取用户列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 用户列表页面
     * @throws Exception 获取用户列表失败
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("user:list")
    public ModelAndView list(HttpServletRequest request,@RequestParam Integer page, @RequestParam Integer size) throws Exception {

        ParamMap paramMap = new ParamMap(request);
        PageInfo<User> pageInfo =  userService.list(paramMap,page,size);

        paramMap.put("pageInfo",pageInfo);
        return new ModelAndView("manager/user/list",paramMap);
    }





    //----------------------------- property ----------------------------------
    @Resource
    private UserService userService;


}

